﻿using U.B.ReadApi.CommandHander.工作流CommandHander;
using WorkflowCore.Interface;
using WorkflowCore.Models;
using U.B.DoMain.工作流引擎;
using U.B.DoMain.工作流引擎.Models;
using IWorkflow = U.B.ReadApi.CommandHander.工作流CommandHander.IWorkflow;

namespace U.B.ReadApi.Command.工作流Command
{
    public class ApprovalWorkflow : IWorkflow
    {
        public string Id => "ApprovalWorkflow";
        public int Version => 1;

        public void Build(IWorkflowBuilder<object> builder)
        {
            builder
               .StartWith<ValidateRequestStep>()
               .Then<CheckPermissionsStep>()
               .Then<ProcessApprovalStep>()
               .Then<NotifyParticipantsStep>()
               .Then<UpdateWorkflowStatusStep>();
        }

        public Task<bool> CompleteApproval(int appId, int result, string reason)
        {
            throw new NotImplementedException();
        }

        public Task<bool> ExecuteApprovalWorkflow(ApprovalWorkflowData data)
        {
            throw new NotImplementedException();
        }

        public Task<bool> StartApprovalProcess(int leaveAppId, int leaderId)
        {
            throw new NotImplementedException();
        }
    }

    public class ValidateRequestStep : StepBody
    {
        public override ExecutionResult Run(IStepExecutionContext context)
        {
            var data = context.Item as ApprovalWorkflowData;
            if (data == null)
            {
                return ExecutionResult.Next();
            }

            if (data.StartTime >= data.EndTime || string.IsNullOrEmpty(data.LeaveReason))
            {
                data.IsValid = false;
                return ExecutionResult.Next();
            }

            data.IsValid = true;
            return ExecutionResult.Next();
        }
    }

    public class CheckPermissionsStep : StepBody
    {
        public override ExecutionResult Run(IStepExecutionContext context)
        {
            var data = context.Item as ApprovalWorkflowData;
            if (data == null)
            {
                return ExecutionResult.Next();
            }

            // 实现权限检查逻辑
            data.HasPermission = true;
            return ExecutionResult.Next();
        }
    }

    public class ProcessApprovalStep : StepBody
    {
        public override ExecutionResult Run(IStepExecutionContext context)
        {
            var data = context.Item as ApprovalWorkflowData;
            if (data == null)
            {
                return ExecutionResult.Next();
            }

            data.CurrentStep = data.AppResult == 1 ? "Approved" : "Rejected";
            return ExecutionResult.Next();
        }
    }

    public class NotifyParticipantsStep : StepBody
    {
        public override ExecutionResult Run(IStepExecutionContext context)
        {
            var data = context.Item as ApprovalWorkflowData;
            if (data == null)
            {
                return ExecutionResult.Next();
            }

            Console.WriteLine($"通知: {data.LeaveName}的请假申请已{data.CurrentStep}");
            return ExecutionResult.Next();
        }
    }

    public class UpdateWorkflowStatusStep : StepBody
    {
        public override ExecutionResult Run(IStepExecutionContext context)
        {
            var data = context.Item as ApprovalWorkflowData;
            if (data == null)
            {
                return ExecutionResult.Next();
            }

            data.LastUpdated = DateTime.Now;
            Console.WriteLine($"工作流状态已更新: {data.CurrentStep}");
            return ExecutionResult.Next();
        }
    }

    public class ValidateInputStep : StepBody
    {
        public override ExecutionResult Run(IStepExecutionContext context)
        {
            // 验证用户输入的用户名和密码是否为空等逻辑
            Console.WriteLine("Validating user input...");
            return ExecutionResult.Next();
        }
    }

    public class CheckUserExistsStep : StepBody
    {
        public override ExecutionResult Run(IStepExecutionContext context)
        {
            // 检查用户是否存在于数据库中的逻辑
            Console.WriteLine("Checking if user exists...");
            return ExecutionResult.Next();
        }
    }

    public class VerifyPasswordStep : StepBody
    {
        public override ExecutionResult Run(IStepExecutionContext context)
        {
            // 验证用户输入的密码是否正确的逻辑
            Console.WriteLine("Verifying password...");
            return ExecutionResult.Next();
        }
    }

    public class RecordLoginLogStep : StepBody
    {
        public override ExecutionResult Run(IStepExecutionContext context)
        {
            // 记录登录日志的逻辑
            Console.WriteLine("Recording login log...");
            return ExecutionResult.Next();
        }
    }

    public class IssueTokenStep : StepBody
    {
        public override ExecutionResult Run(IStepExecutionContext context)
        {
            // 颁发令牌的逻辑
            Console.WriteLine("Issuing token...");
            return ExecutionResult.Next();
        }
    
    }
}
